public class TotalHammingDistance {

    public int totalHammingDistance1(int[] nums) {
        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                ans += Integer.bitCount(nums[i] ^ nums[j]);
            }
        }
        return ans;
    }

    public int totalHammingDistance(int[] nums) {
        int ans = 0;
        for (int i = 0; i < 30; i++) {
            int temp = 0;
            for (int num : nums) {
                temp += (num >> i) & 1;
            }
            ans += temp * (nums.length - temp);
        }
        return ans;
    }
}
